package com.example.questionnaires.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.questionnaires.common.PageResult;
import com.example.questionnaires.common.Result;
import com.example.questionnaires.dto.NewsDTO;
import com.example.questionnaires.entity.News;
import com.example.questionnaires.mapper.NewsMapper;
import com.example.questionnaires.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
import java.time.LocalDateTime;

@Service
@Slf4j
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Override
    public Result<PageResult<NewsDTO>> list(Integer page, Integer size) {
        Page<News> pageParam = new Page<>(page, size);
        Page<News> pageResult = this.page(pageParam, new LambdaQueryWrapper<News>()
            .orderByDesc(News::getCreateTime));

        List<NewsDTO> records = pageResult.getRecords().stream()
            .map(this::convertToDTO)
            .collect(Collectors.toList());

        return Result.success(new PageResult<>(records, pageResult.getTotal(),
            pageResult.getSize(), pageResult.getCurrent()));
    }

    @Override
    public Result<NewsDTO> getById(Long id) {
        News news = super.getById(id);
        if (news == null) {
            return Result.error("新闻不存在");
        }
        return Result.success(convertToDTO(news));
    }

    @Override
    public Result<Void> add(NewsDTO newsDTO) {
        News news = convertToEntity(newsDTO);
        news.setCreateTime(LocalDateTime.now());
        news.setUpdateTime(LocalDateTime.now());
        news.setViews(0);
        super.save(news);
        return Result.success();
    }

    @Override
    public Result<Void> update(NewsDTO newsDTO) {
        News news = convertToEntity(newsDTO);
        news.setUpdateTime(LocalDateTime.now());
        super.updateById(news);
        return Result.success();
    }

    @Override
    public Result<Void> delete(Long id) {
        super.removeById(id);
        return Result.success();
    }

    @Override
    public Result<Void> incrementViews(Long id) {
        News news = super.getById(id);
        if (news != null) {
            news.setViews(news.getViews() + 1);
            super.updateById(news);
        }
        return Result.success();
    }

    @Override
    public Result<List<NewsDTO>> getLatest() {
        List<News> newsList = super.list(new LambdaQueryWrapper<News>()
            .orderByDesc(News::getCreateTime)
            .last("LIMIT 10"));

        List<NewsDTO> dtoList = newsList.stream()
            .map(this::convertToDTO)
            .collect(Collectors.toList());

        return Result.success(dtoList);
    }

    private NewsDTO convertToDTO(News news) {
        NewsDTO dto = new NewsDTO();
        BeanUtils.copyProperties(news, dto);
        return dto;
    }

    private News convertToEntity(NewsDTO dto) {
        News news = new News();
        BeanUtils.copyProperties(dto, news);
        return news;
    }
}
